<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class CaseController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $data = DB::table('cases')->orderBy('weight', 'desc')->get();
        return view('admin/case/index')->with('data', $data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin/case/create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        // 表单验证
        $this->validate($request, [
            'name' => 'required|regex:/.{2,20}/|unique:tags',
        ],[
            'name.required' => '标签不能为空',
            'name.regex' => '标签长度为2-20位',
            'name.unique' => '标签不能重复',
        ]);

        $data = $request->except('_token');
        $data['weight'] = empty($data['weight']) ? 0 : $data['weight'];
        if (DB::table('cases')->insert($data)) {
            return redirect('/admin/case/index');
        } else {
            return redirect('/admin/case/edit/'.$data['id']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $olddata = DB::table('cases')->find($id);
        return view('admin/case/edit')->with('olddata', $olddata);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        // 表单验证
        $rule = new \Illuminate\Validation\Rule;
        $this->validate($request, [
            'name' => 'required|regex:/\w{2,20}/',
            'name' => $rule->unique('cases')->ignore($id),
        ],[
            'name.required' => '专栏名称不能为空',
            'name.regex' => '专栏名称长度为2-20位',
            'name.unique' => '专栏名称不能重复',
        ]);

        $data = $request->except('_token');
        $data['weight'] = empty($data['weight']) ? 0 : $data['weight'];
        if (DB::table('cases')->where('id', '=', $id)->update($data)) {
            return redirect('/admin/case/index');
        } else {
            return redirect('/admin/case/edit/'.$data['id']);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        DB::table('cases')->where('id', '=', $id)->delete();
        return redirect('/admin/case/index');
    }

    /**
     * 批量删除
     */
    public function dels(Request $request)
    {
        if($ids = $request->input('ids')){
            DB::table('cases')->whereIn('id', $ids)->delete();
        }
        return redirect('/admin/case/index');
    }
}
